package com.wang;


import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class MainTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String pkgName = "java.util.";
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String path = pkgName.replace(".", "/");
        Enumeration<URL> resources = cl.getResources(path);
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            String protocol = resource.getProtocol();
            if ("file".equals(protocol)) {
                File file = new File(resource.getFile());
                System.out.println(findClasses(file, "java.util").size());
            } else if ("jar".equals(protocol)) {
                System.out.println("jar类型的扫描");
                String jarpath = resource.getPath();
                jarpath = jarpath.replace("file:/", "");
                jarpath = jarpath.substring(0, jarpath.indexOf("!"));
                System.out.println(getClasssFromJarFile(jarpath, path).size());
            }
        }
    }


    private static List<Class<?>> findClasses(File directory, String packageName)
            throws ClassNotFoundException {
        ArrayList<Class<?>> classes = new ArrayList<Class<?>>();

        if (!directory.exists()) {
            return classes;
        }

        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file,
                        packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {
                classes.add(Class.forName(packageName
                        + "."
                        + file.getName().substring(0,
                        file.getName().length() - 6)));
            }
        }
        return classes;

    }

    /**
     * 从jar文件中读取指定目录下面的所有的class文件
     *
     * @param jarPaht  jar文件存放的位置
     * @param filePaht 指定的文件目录
     * @return 所有的的class的对象
     */
    public static List<Class<?>> getClasssFromJarFile(String jarPaht,
                                                      String filePaht) throws IOException {
        List<Class<?>> clazzs = new ArrayList<Class<?>>();

        JarFile jarFile = null;
        jarFile = new JarFile(jarPaht);

        List<JarEntry> jarEntryList = new ArrayList<JarEntry>();

        Enumeration<JarEntry> ee = jarFile.entries();
        while (ee.hasMoreElements()) {
            JarEntry entry = ee.nextElement();
            // 过滤我们出满足我们需求的东西
            if (entry.getName().startsWith(filePaht)
                    && entry.getName().endsWith(".class")) {
                jarEntryList.add(entry);
            }
        }
        for (JarEntry entry : jarEntryList) {
            String className = entry.getName().replace('/', '.');
            className = className.substring(0, className.length() - 6);

            try {
                clazzs.add(Thread.currentThread().getContextClassLoader()
                        .loadClass(className));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return clazzs;
    }

}
